/**
 * @author KujoMiva
 * @name Music.ts
 * @desc 音乐播放组件
 * @time 2020年02月08日 19:03:47 星期六
 */
class Music extends egret.DisplayObjectContainer {
  private bgm
  private soundChannel = null
  private btn_sound = Utils.createBitmapByName('music_off_png')
  public constructor() {
    super()
    this.once(egret.Event.ADDED_TO_STAGE, this.init, this)
  }
  private init() {
    this.addChild(this.btn_sound)
    this.loadBgm()
    this.createBtnPlay()
    // this.createMask()
  }
  // 添加遮罩请求开启bgm
  public createMask() {
    const _mask = new egret.Sprite()
    _mask.graphics.beginFill(0, .5)
    _mask.graphics.drawRect(0, 0, this.stage.stageWidth, this.stage.stageHeight)
    _mask.graphics.endFill()
    this.addChild(_mask)
    const text = Utils.createText('需要开启音乐,点击同意', { x: 185, y: 525 }, this, false)
    text.textColor = 0xffffff
    text.size = 28
    _mask.touchEnabled = true
    _mask.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
      console.log('播放bgm')
      this.createBtnPause()
      this.soundChannel = this.bgm.play()
      this.removeChild(_mask)
      this.removeChild(text)
      $('.item-mask').hide()
    }, this)
  }
  // 加载背景音乐资源
  private loadBgm() {
    this.bgm = RES.getRes('bgm_mp3')
  }
  // 创建播放按钮
  private createBtnPlay() {
    this.zIndex = 100
    // const btn_sound = new egret.Shape()
    const btn_sound = Utils.createBitmapByName('music_off_png')
    this.removeChild(this.btn_sound)
    this.addChild(btn_sound)
    this.btn_sound = btn_sound

    // this.btn_sound.graphics.lineStyle(5, 0xffffff)
    // this.btn_sound.graphics.beginFill(0, .3)
    // this.btn_sound.graphics.drawArc(50, 50, 30, 0, Math.PI / 180 * 360, false)
    // this.btn_sound.graphics.endFill()
    // this.btn_sound.graphics.moveTo(45, 35)
    // this.btn_sound.graphics.lineTo(45, this.btn_sound.height)
    // this.btn_sound.graphics.endFill()
    // this.btn_sound.graphics.moveTo(55, 35)
    // this.btn_sound.graphics.lineTo(55, this.btn_sound.height)
    // this.btn_sound.graphics.endFill()

    this.btn_sound.touchEnabled = true
    this.btn_sound.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
      console.log('播放bgm')
      this.createBtnPause()
      this.soundChannel = this.bgm.play()
    }, this)

    this.settingBenPath()
  }
  // 创建暂停按钮
  private createBtnPause() {
    // const btn_sound = new egret.Shape()
    const btn_sound = Utils.createBitmapByName('music_on_png')
    this.removeChild(this.btn_sound)
    this.addChild(btn_sound)
    this.btn_sound = btn_sound

    // this.btn_sound.graphics.lineStyle(5, 0xffffff)
    // this.btn_sound.graphics.beginFill(0, .3)
    // this.btn_sound.graphics.drawArc(50, 50, 30, 0, Math.PI / 180 * 360, false)
    // this.btn_sound.graphics.endFill()
    // this.btn_sound.graphics.lineStyle(5, 0xffffff)
    // this.btn_sound.graphics.drawArc(40, 50, 0, -45, Math.PI / 180 * 60, false)
    // this.btn_sound.graphics.endFill()
    // this.btn_sound.graphics.lineStyle(5, 0xffffff)
    // this.btn_sound.graphics.drawArc(40, 50, 10, -45, Math.PI / 180 * 60, false)
    // this.btn_sound.graphics.endFill()
    // this.btn_sound.graphics.lineStyle(5, 0xffffff)
    // this.btn_sound.graphics.drawArc(40, 50, 20, -45, Math.PI / 180 * 60, false)
    // this.btn_sound.graphics.endFill()

    this.btn_sound.touchEnabled = true
    this.btn_sound.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
      console.log('暂停bgm')
      this.createBtnPlay()
      this.soundChannel.stop()
      this.soundChannel = null
    }, this)

    this.settingBenPath()
  }
  // 设置按钮位置
  private settingBenPath() {
    Utils.anchorCenter(this.btn_sound)
    this.btn_sound.x = this.stage.stageWidth - this.btn_sound.width - 0
    this.btn_sound.y = 50
  }
}
